package com.eportal.action.background;

import com.eportal.action.BaseAction;
import com.eportal.domain.*;
import com.eportal.service.*;
import com.eportal.util.*;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;


public class BackGroundAction extends BaseAction implements ModelDriven<Sysopt> {

	static MdExpiringMap<String, String> qrcodeMap = new MdExpiringMap(20*60,60);

	static MdExpiringMap<String, String> appLoginMap = new MdExpiringMap(30*60,60);
    /**
	 * @author chencheng
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(BackGroundAction.class);
	@Autowired
    SysOptService sysOptService;
	@Autowired
    SysmenuService sysmenuService;
	@Autowired
    private TAgentService tAgentService;
	@Autowired
    private TCommunityService tCommunityService;
	@Autowired
	private TUsersService tUsersService;
	@Autowired
	private CityService cityService;

	public String login() {
		String k = getUniqueKey(qrcodeMap);
		getRequest().setAttribute("k", k);
        return "login";
    }


	String getUniqueKey(MdExpiringMap map){
		while(true){
			String k = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
			log.info("getUniqueKey k="+k);
			if(map.get(k)==null){
				return k;
			}
		}
	}


	public String login4c() {
        return "login4c";
    }


	public String zlindex() {
		return "zlindex";
	}
	public String partner() {
		return "partner";
	}

	public String index() {
		try{
			int userRole = (Integer) getSession().getAttribute("userRole");
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			if (userRole == 1 || userRole == 2) {
				getSession().setAttribute("selfname", "掌连云管理后台");
				getRequest().setAttribute("selfname", "掌连云管理后台");
				getSession().setAttribute("communityName", "掌连云管理后台");
				getRequest().setAttribute("communityName", "掌连云管理后台");
				getMenu();
				return "homepage";
			}else if (userRole ==21) {
				getSession().setAttribute("selfname", "掌连云管理后台");
				getRequest().setAttribute("selfname", "掌连云管理后台");
				getSession().setAttribute("communityName", "掌连云管理后台");
				getRequest().setAttribute("communityName", "掌连云管理后台");
				getMenu();
				return "homepage";
			} else if (userRole == 3 || userRole == 4 || userRole == 7) { //主板厂商todo Updated by Saber on 2017/7/25 下午4:33
				StringBuffer sb = new StringBuffer("where t.agtcode='");
				sb.append(user.getAgtCode());
				sb.append("'");
				List<Map<String, String>> agtList = tAgentService.getDeatil(sb.toString());
				Map<String, String> map = agtList.get(0);
				getSession().setAttribute("selfname", agtList.get(0).get("agtName"));
				getRequest().setAttribute("selfname", agtList.get(0).get("agtName"));
				getSession().setAttribute("communityName", agtList.get(0).get("agtName"));
				getRequest().setAttribute("communityName", agtList.get(0).get("agtName"));
				if (!"1".equals(map.get("status"))) {// 未审核通过

					String firstLogin = "1";
					if("2".equals(agtList.get(0).get("app_type"))&&"1".equals(firstLogin)){
						getRequest().setAttribute("isTipCustom","1");//
					}
					getRequest().setAttribute("map", agtList.get(0));
					getRequest().setAttribute("mpid","all");
					return "tagentDetailView";
				} else if (user.getrLevel() == 1) {// 一般合作方主页
					getRequest().setAttribute("agtCode", user.getAgtCode());
					getMenu();
					return "homepage";
				}else if(user.getrLevel() == 6){//主板合作方主页 Created by Saber on 2017/7/25 下午4:42
					getRequest().setAttribute("userRole", userRole);
					getRequest().setAttribute("agtCode", user.getAgtCode());
					getRequest().setAttribute("rLevel", user.getrLevel());
					getMenu();
					return "homepage";
				} else {// 锁厂主页
					getRequest().setAttribute("userRole", userRole);
					getRequest().setAttribute("agtCode", user.getAgtCode());
					getRequest().setAttribute("rLevel", user.getrLevel());
					return "tcommunityListView";
				}
			} else if (userRole == 5 || userRole == 6) {
				TAgent tagent = tAgentService.get(user.getAgtCode());
				getSession().setAttribute("selfname", tagent.getAgtName());
				getRequest().setAttribute("selfname", tagent.getAgtName());
				getRequest().setAttribute("agtName", tagent.getAgtName());
				Map<String, String> tCommunity = tCommunityService.getBySql(Long.parseLong(user.getCommunityId()));
				getSession().setAttribute("communityName", tCommunity.get("communityName"));
				getRequest().setAttribute("communityName", tCommunity.get("communityName"));
				if (!"1".equals(tCommunity.get("status"))) {// 未审核通过
					getRequest().setAttribute("tCommunity", tCommunity);
					return "tcomuDetailView";
				} else if (user.getrLevel() == 2) {// 一般社区主页
					getRequest().setAttribute("tCommunityId", user.getCommunityId());
					getMenu();
					return "homepage";
				} else {// 锁厂客户的主页
					getRequest().setAttribute("communityId", user.getCommunityId());
					getRequest().setAttribute("agtCode", user.getAgtCode());
					getRequest().setAttribute("userRole", userRole);
					getRequest().setAttribute("rLevel", user.getrLevel());
					return "tusersListView";
				}
			} else {
				return "error";
			}
		}catch(Exception e){
			log.error(this, e);
			return "error";
		}


	}

	/**
	 * 首页快捷图标
	 */
	 private void getMenu() {
		//报表
			int userRole = (Integer) getSession().getAttribute("userRole");
			Sysopt user = (Sysopt) getSession().getAttribute("user");
			List<Map<String, String> > dataList = new ArrayList<Map<String, String> >();
			dataList = (List<Map<String, String>>) getSession().getAttribute("dataList");
			if (dataList == null) {
				if (userRole == 1 || userRole == 2 || userRole == 21) {//平台报表;
					dataList = tAgentService.getPmsReports(userRole,user);
				}else if(userRole == 3 || userRole == 4){//合作方报表
					dataList = tAgentService.tagentReports(user.getAgtCode());
				}else{//社区报表
					dataList = tAgentService.tcomuReports(user.getAgtCode(),user.getCommunityId());
				}
				//TODO改成json之后，其他地方没有相应的修改
				//getSession().setAttribute("dataList", com.alibaba.fastjson.JSONArray.toJSONString(dataList));
				getSession().setAttribute("dataList", dataList);
			}
			JSONArray json = JSONArray.fromObject(dataList);
			getRequest().setAttribute("dataList", json.toString());
			getRequest().setAttribute("userRole", userRole);
			//menu为模块
			Map<String, Object> map = (Map<String, Object>) getSession().getAttribute("auths");
	        List<Sysmenu> sysmenus = (List<Sysmenu>) map.get("menus");
	        List<Sysmenu> homemenus = new ArrayList<Sysmenu>();
	        for (Sysmenu menu : sysmenus) {
	        	if (!"0".equals(menu.getmPno())) {
	        		continue;
				}
	        	if(StringUtils.isNotBlank(menu.getmUrl())){
	        		String[] iteam = menu.getmUrl().split(",");
	        		menu.setmUrl(iteam[0]);
	        	}
	        	homemenus.add(menu);
	        }
	        getRequest().setAttribute("homemenus", homemenus);

	}

	public String defaultIndex() {
		 String mpid=getRequest().getParameter("mpid");
		 getRequest().setAttribute("mpid", Integer.parseInt(mpid));
		 getSession().setAttribute("mpid", Integer.parseInt(mpid));
		 getRequest().setAttribute("first","first");
	     List<Sysmenu> sysmenus = null;
         Map<String, Object> map = (Map<String, Object>) getSession().getAttribute("auths");
 		 sysmenus = (List<Sysmenu>) map.get("menus");
 		 for (Sysmenu menu : sysmenus) {
 			 if(mpid.equals(menu.getmPno())){
 				 String mUrl = menu.getmUrl().split(",")[0];
 				 getRequest().setAttribute("murl",mUrl);
 				 getSession().setAttribute("murl",mUrl);
 				 break;
 			 }
 		 }
	     return "index";
	}

    public String findPassword() {
    	return "findPassword";
    }


    /**
     * 管理员登录
     * @throws IOException
     */
    public void doLogin() throws IOException {
    	Map<String, Object> param = new HashMap<String, Object>();
    	Map<String, Object> result = new HashMap<String, Object>();
    	try{
        	param.put("userName", getModel().getUserName().trim().replace(" ", ""));
            param.put("passwd", MD5.MD5Encode(getModel().getPasswd()));
        	result = sysOptService.loginMethod(param);

        	Sysopt sysopt = (Sysopt)getRequest().getSession().getAttribute("user");
        	if(sysopt!=null&&sysopt.getrLevel()==4&&sysopt.getAppUserid()==null){//锁厂顾客且没有绑定过appUserid的,需要绑定
        		param.put("needQrcodeBind", "1");
        		String k = UUID.randomUUID().toString().replaceAll("-", "");
        		qrcodeMap.put(k,sysopt.getSid().toString());
        		param.put("qrcodeKey", k);
        		param.put("qrcodeStr", WebUtil.getReqPreUrl(getRequest())+"admin/bindAppUserByQrcode.action?k="+k);
        	}
    	}catch(Exception e){
    		log.error(this, e);
    		 param.put("code", 0);
             param.put("msg", "系统繁忙");
    	}


        responseBody(result);
    }

	/**
	 * 获取 userRole
	 * @author Saber
	 * @Date 2017/7/31 下午4:49
	 */
	public void getUserRole(){
		Map<String,Object> result = new HashMap<>();
		int userRole = (Integer) getSession().getAttribute("userRole");
		result.put("code",1);
		result.put("msg","ok");
		result.put("data",userRole);
		responseBody(result);
	}

    /**
     * 通过api登录
     * @throws IOException
     */
    public void doLoginByApi() throws IOException {
    	Map<String, Object> param = new HashMap<String, Object>();
    	Map<String, Object> result = new HashMap<String, Object>();
    	String sign_ = MD5.MD5Encode(getModel().getUserName()+getModel().getPasswd()+"qwertyuio23456789zxcvbnmlkjhgf");
    	try{
    		String sign = getRequest().getParameter("sign");
    		if(sign_.equals(sign)){
    			param.put("userName", getModel().getUserName().trim().replace(" ", ""));
                param.put("passwd", getModel().getPasswd());
            	result = sysOptService.loginMethod(param);
    		}else{
    			result.put("code", 0);
    			result.put("msg", "signature is wrong");
    		}

    	}catch(Exception e){
    		log.error(this, e);
    		result.put("code", 0);
			result.put("msg", "login has error");
    	}
        responseBody(result);
    }

    /**
     *
     * 绑定web用户和app用户，手机端扫二维码后显示的页面
     * @return
     * @throws IOException
     */
    public String bindAppUserByQrcode() throws IOException {
    	try{
    		String appUserid = getRequest().getParameter("userid");
        	String token = getRequest().getParameter("token");
        	String random = getRequest().getParameter("random");

        	if(random!=null){
        		if(appLoginMap.get(random)==null){
        			appLoginMap.put(random, "");
        		}else{
        			return "close";
        		}
        	}

    		String k=getRequest().getParameter("k");
    		if(appUserid==null){
    			getResponse().sendRedirect(Config.MD_OPEN_TSHELVES_URL+"/app/authWebLogin.do?url="+URLEncoder.encode(WebUtil.getReqPreUrl(getRequest())+"admin/bindAppUserByQrcode.action?random="+UUID.randomUUID().toString()+"&k="+k));
    		}else{
    			if(tUsersService.checkUser4WebAuth(appUserid, token)){
    				getRequest().getSession().setAttribute("app_userid", appUserid);
    				getRequest().setAttribute("k",k);
    	    		String val = qrcodeMap.get(k);
    	    		if(val!=null&&val.length()>0){
    	    			Sysopt opt = sysOptService.get(Long.valueOf(val));
    	    			if(opt!=null){
    	    				getRequest().setAttribute("uname", opt.getUname());
    	    				String cityname = "";
    	    				try{
    	    					getRequest().setAttribute("addr", opt.getAddr());
    	        				TCommunity community =  tCommunityService.get(Long.valueOf(opt.getCommunityId()));
    	        				City city = (City)cityService.get("from City where dm='"+community.getAreaCode()+"'");
    	        				cityname = city.getDescribed();
    	    				}catch(Exception e){
    	    					log.error(this, e);
    	    				}
    	    				getRequest().setAttribute("cityname",cityname);
    	    				getRequest().setAttribute("tel",opt.getTel());
    	    			}else{
    	    				return "exception";
    	    			}
    	    		}else{
    	    			getRequest().setAttribute("msg", "二维码失效，请重新刷新");
    	    			return "exception";

    	    		}
    			}else{
    				getRequest().setAttribute("msg", "用户校验失败，请重新尝试");
	    			return "exception";
    			}
    		}




    	}catch(Exception e){
    		log.error(this, e);
    	}

		return SUCCESS;
    }


    /**
     * 手机端，点击确认绑定
     * @throws IOException
     */
    public void yesBindByQrcode() throws IOException {
    	Map map = new HashMap();
    	try{
    		String k=getRequest().getParameter("k");
    		String code=getRequest().getParameter("code");
    		String appUserid = (String)getRequest().getSession().getAttribute("app_userid");
    		log.info("yesBindByQrcode appUserid="+appUserid);
    		String val = qrcodeMap.get(k);
    		if(val!=null&&val.length()>0){
    			Sysopt opt = sysOptService.get(Long.valueOf(val));
    			log.info("yesBindByQrcode val="+val);
    			if(SmsHelper.checkSMSValidCache(opt.getTel(), code)){
    				if(opt.getAppUserid()==null){
        				Sysopt other = sysOptService.getByAppUserid(appUserid);//检查该app用户是否绑定其他用户，app用户和管理员用户，应该是1对1的关系
        				if(other==null){
        					TUsers users = tUsersService.getByUserid(appUserid);
        					if(users.getAgtCode().equals(opt.getAgtCode())){
        						opt.setAppUserid(appUserid);
            					sysOptService.update(opt);
            					map.put("code", "0");
                    			map.put("msg", "绑定成功，请扫描登录二维码进入");
        					}else{
        						map.put("code", "1");
                    			map.put("msg", "账户信息不匹配，绑定失败");
        					}
        				}else{
        					map.put("code", "0");
                			map.put("msg", "该app用户已绑定了其他账户，不能重复绑定");
        				}
        			}else{
        				if(appUserid.equals(opt.getAppUserid())){
        					map.put("code", "1");
        					map.put("msg", "绑定成功，请扫描登录二维码进入");
        				}else{
        					map.put("code", "0");
                			map.put("msg", "该用户已绑定");
        				}
        			}
    			}else{
    				map.put("code", "0");
        			map.put("msg", "验证码错误，请确认信息");
    			}
    			//getRequest().setAttribute("", arg1);
    		}else{
    			map.put("code", "0");
    			map.put("msg", "二维码过期或者已失效，请重新刷新");
    		}

    	}catch(Exception e){
    		log.error(this, e);
    		map.put("code", "-999");
			map.put("msg", "服务繁忙，请稍后再试");
    	}
    	log.info(JSONObject.fromObject(map).toString());
    	responseBody(map);
    	return ;
    }



    /**
     * app登录
     * @throws IOException
     */
    public void loginByApp() throws IOException {

    	log.info("----------loginByApp-----------");
    	getResponse().sendRedirect(Config.MD_OPEN_TSHELVES_URL+"/app/authWebLogin.do?url="+URLEncoder.encode(WebUtil.getReqPreUrl(getRequest())+"admin/doLoginByApp.action?random="+UUID.randomUUID().toString()));


		return;
    }


    /**
     * app登录
     * @throws IOException
     */
    public String doLoginByApp() throws IOException {
    	log.info("----------doLoginByApp-----------");
    	try{
    		String appUserid = getRequest().getParameter("userid");
        	String token = getRequest().getParameter("token");
        	String random = getRequest().getParameter("random");
        	String loginedFlag = getRequest().getParameter("loginedFlag");
        	//TUsers users = tUsersService.getByUserid(appUserid);
        	String reqUri = getRequest().getParameter("reqUri");

        	if(random!=null){
        		if(appLoginMap.get(random)==null){
        			appLoginMap.put(random, "");
        		}else{
        			return "close";
        		}
        	}
        	log.info("reqUri="+reqUri);
        	Map<String, Object> result =  new HashMap();
        	if("0".equals(loginedFlag)){
        		log.info("------未登录的");
    			getResponse().setStatus(301);
    			getRequest().setAttribute("loginedFlag", "0");
    			getRequest().getSession().setAttribute("app_userid", null);
				getResponse().sendRedirect(WebUtil.getReqPreUrl(getRequest())+"register/registerLockApp.action");
				return null;
        	}else if(tUsersService.checkUser4WebAuth(appUserid, token)){//检验userid和token
        		getRequest().getSession().setAttribute("app_userid", appUserid);
        		Sysopt opt = sysOptService.getByAppUserid(appUserid);
        		if(opt!=null){
        			result = sysOptService.loginBySysopt(opt);
        			if(new Integer(1).equals(result.get("code"))){
        				log.info("doLoginByApp success");
        				if(reqUri!=null&&reqUri.length()>0){
        					log.info("reqUri");
        					//return "forword:"+URLDecoder.decode(forwordUrl);
        					getResponse().sendRedirect(URLDecoder.decode(reqUri));
        	        		return null;
        				}else{
        					getResponse().setStatus(301);
        					//getResponse().sendRedirect(WebUtil.getReqPreUrl(getRequest())+"admin/tlocks_lockManagerApp.action");
        					getResponse().sendRedirect(Config.MD_OPEN_TSHELVES_URL+"/lifes/lock/lock_grammer.do");
        					return null;
        				}

        			}else{
        				getRequest().setAttribute("msg", result.get("msg"));
                		return "exception";
        			}
        		}else{
        			log.info("---------请先注册");
        			getResponse().setStatus(301);
					getResponse().sendRedirect(WebUtil.getReqPreUrl(getRequest())+"register/registerLockApp.action");
					return null;
        		}
        	}else{
        		getRequest().setAttribute("msg", "用户信息异常，请重新尝试");
        		return "exception";
        	}
    	}catch(Exception e){
    		log.error(this, e);
    	}
    	return "exception";
    }

    /**
     *
     * 手机端扫二维码后显示的页面
     * loginByQrcode
     * @return
     * @throws IOException
     */
    public String lbq() throws IOException {
    	try{
    		String k=getRequest().getParameter("k");
        	if(k!=null){
        		getRequest().getSession().setAttribute("qrcodeKey", k);
        	}
        	String appUserid = (String) getRequest().getSession().getAttribute("app_userid");
        	Sysopt user = (Sysopt) getSession().getAttribute("user");
        	log.info("----------loginByQrcode-----------appUserid="+appUserid+"user="+user);
        	boolean checkOptAndAppUserid = false;//检测appUserid和user是否还是绑定关系,因为数据变化,内存中获取的数据不一定正确
        	try{
        		if(appUserid!=null&&user!=null){
            		Sysopt opt = sysOptService.get(Long.valueOf(user.getSid()));
            		if(appUserid.equals(opt.getAppUserid())){
            			checkOptAndAppUserid = true;
            		}
            	}
        	}catch(Exception e){
        		log.error(this, e);
        	}
        	if(appUserid==null||user==null||checkOptAndAppUserid==false){
        		log.info("----------loginByQrcode----------Redirect");
        		String reqUri=URLEncoder.encode(WebUtil.getReqPreUrl(getRequest())+"admin/loginByQrcode.action");
        		getResponse().sendRedirect(Config.MD_OPEN_TSHELVES_URL+"/app/authWebLogin.do?url="+URLEncoder.encode(WebUtil.getReqPreUrl(getRequest())+"admin/doLoginByApp.action?random="+UUID.randomUUID().toString()+"&reqUri="+reqUri));
        		return null;
        	}else{
        	}
    	}catch(Exception e){
    		log.error(this, e);
    	}

		return SUCCESS;
    }



    /**
     * 手机端，点击确认登录
     * @throws IOException
     */
    public void yesLoginByQrcode() throws IOException {
    	Map map = new HashMap();
    	try{
        	String qk = (String)getRequest().getSession().getAttribute("qrcodeKey");
        	log.info("qk="+qk);
        	Sysopt user = (Sysopt) getSession().getAttribute("user");
        	String appUserid = (String)getSession().getAttribute("app_userid");
        	boolean checkOptAndAppUserid = false;//检测appUserid和user是否还是绑定关系,因为数据变化,内存中获取的数据不一定正确
        	try{
        		if(appUserid!=null&&user!=null){
            		Sysopt opt = sysOptService.get(Long.valueOf(user.getSid()));
            		if(appUserid.equals(opt.getAppUserid())){
            			checkOptAndAppUserid = true;
            		}
            	}
        	}catch(Exception e){
        		log.error(this, e);
        	}

        	if(checkOptAndAppUserid){
        		if(user!=null){
        			log.info("user.getSid()="+user.getSid());
        			qrcodeMap.put(qk, user.getSid().toString());
        			boolean loginSuccess=false;
        			long start = System.currentTimeMillis();
        			long now = System.currentTimeMillis();
        			while(now-start<3000){
        				log.info(qrcodeMap.get(qk+"_success"));
        				if("1".equals(qrcodeMap.get(qk+"_success"))){
        					loginSuccess = true;
        					break;
        				}else{
        					now = System.currentTimeMillis();//不断更新now
        					Thread.sleep(200);
        				}
        			}
        			if(loginSuccess){
        				map.put("code", "1");
        			}else{
        				map.put("code", "-999");
            			map.put("msg", "登录失败，请重新尝试或者刷新页面");
        			}


        		}else{
        			map.put("code", "-999");
        			map.put("msg", "登录失败，请重新尝试或者刷新页面");
        		}
        	}else{
        		map.put("code", "-999");
    			map.put("msg", "用户信息不符，请重新尝试或者刷新页面");
        	}
    	}catch(Exception e){
    		log.error(this, e);
    		map.put("code", "-999");
			map.put("msg", "服务繁忙，请稍后再试");
    	}
    	log.info(JSONObject.fromObject(map).toString());
    	responseBody(map);
    	return ;
    }


    /**
     * web端循环请求，判断手机端是否确认登录
     * @throws IOException
     */
    public void doLoginByQrcode() throws IOException {
    	log.info("----------doLoginByQrcode-----------");
    	Map result = new HashMap();
    	try{
    		String qk =getRequest().getParameter("k");
    		String sid = qrcodeMap.get(qk);

    		if(sid!=null&&sid.length()>0){
    			Sysopt opt = sysOptService.get(Long.valueOf(sid));
    			if(opt!=null){
    				result=sysOptService.loginBySysopt(opt);
    				qrcodeMap.put(qk+"_success", "1");
    			}else{
    				result.put("code", "-999");
    				result.put("msg", "服务繁忙，请稍后再试");
    			}
    		}else{
    			result.put("code", "-100");
				result.put("msg", "");
    		}

    	}catch(Exception e){
    		log.error(this, e);
    	}
    	responseBody(result);

    }


    /**
     * 发送验证码
     */
    public void sendMessage() {
    	 String phone = getRequest().getParameter("phone");
    	 String username = getRequest().getParameter("username");
    	 List<Sysopt> list = sysOptService.getByUserName(username);
    	 Map<String, Object> result = new HashMap<String, Object>();
    	 if(list.size()>0){
    		 Sysopt opt = list.get(0);
    		 if(phone!=null&&phone.equals(opt.getTel())){
    			 result = new HashMap<String, Object>();
    	    	 result = sysOptService.sendSMSVaildCodeMethod(phone);
    		 }else{
    			 result.put("code", 0);
    			 result.put("msg", "输入的手机号与账户绑定的手机号不符合,请重新确认");
    		 }
    	 }else{
    		 result.put("code", 0);
			 result.put("msg", "输入的账号不存在");
    	 }
    	 responseBody(result);
    }

    /**
     * 发送验证码
     */
    public void sendCode4BindApp() {
    	 String phone = getRequest().getParameter("phone");
    	 String appid = getRequest().getParameter("appid");
    	 Map<String, Object> result = new HashMap<String, Object>();
		 result = new HashMap<String, Object>();
		 boolean isUserExit = false;
		 if(appid!=null){
			 TUsers tUsers = tUsersService.getByWhere(" appid='"+appid+"' and tel='"+phone+"'");
			 if(tUsers!=null){
				 isUserExit = true;
			 }
		 }
    	 result = sysOptService.sendSMSVaildCodeMethod(phone);
    	 if(isUserExit){
    		 result.put("isUserExit", "1");
    	 }else{
    		 result.put("isUserExit", "0");
    	 }
    	 responseBody(result);
    }

    /**
     * 重置密码
     */
	public void resetPassword() {
		Map<String, Object> result = new HashMap<String, Object>();
		String yzm = getRequest().getParameter("yzm");
		List<Sysopt> list = sysOptService.getByUserName(getModel().getUserName());
		if (list.size() > 0 && list.get(0).getTel().equals(getModel().getTel())) {
			Sysopt sysopt = getModel();
			boolean bool = SmsHelper.checkSMSValidCache(sysopt.getTel(), yzm);
			if (!bool) {
				result.put("code", 0);
				result.put("msg", "验证码不正确");
				responseBody(result);
				return;
			}
			result = sysOptService.resetPasswordMethod(sysopt);
		} else {
			result.put("code", 0);
			result.put("msg", "输入的手机号与账户绑定的手机号不符合,请重新确认");
		}

		responseBody(result);
	}


	public void genVerifyCode() {
		try {
			VerifyCodeHelper.createCode("FUNC_WALLET_ROLLOUT", 100, 50, getRequest().getSession(), getResponse().getOutputStream());


		} catch (IOException e) {
			log.error(this, e);
		}

	}



    @Override
    public Sysopt getModel() {
        return user;
    }

    private Sysopt user = new Sysopt();

}
